package GUI.lecturerGUI;

import java.awt.Component;
import java.awt.SystemColor;
import java.awt.color.CMMException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;

import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JComboBox;

import common.Perror;

import controllers.LecturerController;
import entities.Course;
import entities.Exam;
import entities.ExtractedExam;

import GUI.MainGUI;
import javax.swing.JTextArea;

/**
 * the Lecturer Generate new Exam Code GUI
 */
public class LecturerGenerateExamCode extends JPanel implements ActionListener,
		FocusListener, KeyListener {

	private static final long serialVersionUID = 1L;
	private JTextField txtGenerateCode;
	private JTextArea txtExamComts;
	private JButton btnGenerateExam;
	private JButton btnExit;
	private JButton btnMainMenu;

	private JLabel lblEnterNewCode;
	private JLabel lblChooseExam;

	private JComboBox cboxExams;

	private LecturerController lecturerCtrl;
	private ArrayList<ExtractedExam> examLst;
	private ExtractedExam selectedExam;

	public LecturerGenerateExamCode(LecturerController lecturerCtrl) {

		super();
		this.lecturerCtrl = lecturerCtrl;
		initialize();

	}

	/**
	 * initializing the GUI
	 */
	private void initialize() {
		setBorder(new TitledBorder(null, "Lecturer Generate Exam Code : ",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setBackground(SystemColor.activeCaption);
		setBounds(0, 0, MainGUI.setX, MainGUI.setY);
		setLayout(null);
		examLst = new ArrayList<ExtractedExam>();
		add(GETtxtGenerateCode());
		add(GETbtnGenerateExam());
		add(GETbtnExit());
		add(GETbtnMainMenu());
		add(GETlblEnterNewCode());
		add(GETlblChooseExam());
		add(GETcmbBxExams());
		add(GETtxtExamComts());

	}

	private JTextArea GETtxtExamComts() {
		if (txtExamComts == null){
		txtExamComts = new JTextArea();
		txtExamComts.setBounds(19, 130, 436, 181);
		txtExamComts.setEditable(false);
		}
		return txtExamComts;
	}

	private JTextField GETtxtGenerateCode() {
		if (txtGenerateCode == null) {
			txtGenerateCode = new JTextField();
			txtGenerateCode
					.setToolTipText("Code must be 4 chars long and contain at least 1 letter and 1 number");
			txtGenerateCode.setText("Enter exam code");
			txtGenerateCode.setBounds(164, 352, 109, 20);
			txtGenerateCode.setColumns(10);
			txtGenerateCode.addFocusListener(this);
			txtGenerateCode.addKeyListener(this);
		}
		return txtGenerateCode;
	}

	private JButton GETbtnGenerateExam() {
		if (btnGenerateExam == null) {
			btnGenerateExam = new JButton("Generate exam");
			btnGenerateExam.setBounds(308, 440, 147, 49);
			btnGenerateExam.setEnabled(false);
			btnGenerateExam.addActionListener(this);
		}
		return btnGenerateExam;
	}

	private JButton GETbtnExit() {
		if (btnExit == null) {
			btnExit = new JButton("EXIT");
			btnExit.setBounds(148, 459, 90, 30);
			btnExit.addActionListener(this);
		}
		return btnExit;
	}

	private JButton GETbtnMainMenu() {
		if (btnMainMenu == null) {
			btnMainMenu = new JButton("Main Menu");
			btnMainMenu.setBounds(19, 459, 119, 30);
			btnMainMenu.addActionListener(this);

		}
		return btnMainMenu;
	}

	private JLabel GETlblEnterNewCode() {
		if (lblEnterNewCode == null) {
			lblEnterNewCode = new JLabel("Enter code for exam:");
			lblEnterNewCode.setBounds(19, 352, 141, 20);
		}
		return lblEnterNewCode;
	}

	private JLabel GETlblChooseExam() {
		if (lblChooseExam == null) {
			lblChooseExam = new JLabel("Choose exam:");
			lblChooseExam.setBounds(19, 26, 129, 35);
		}
		return lblChooseExam;
	}

	private JComboBox GETcmbBxExams() {
		if (cboxExams == null) {
			cboxExams = new JComboBox();
			cboxExams.setBounds(29, 68, 431, 20);
		}
		return cboxExams;
	}
/**
 * add exam to combo
 * @param exm list of exams
 */
	public void addExams(ArrayList<ExtractedExam> exm) {
		examLst.clear();
		this.examLst.addAll(0, exm);
		cboxExams.removeAllItems();
		for (int i = 0; i < (exm.size()); i++) {
			ExtractedExam extExm = examLst.get(i);
			cboxExams.addItem(extExm.getSubjectID() > 10 ? extExm
					.getSubjectID() : ("0" + extExm.getSubjectID())
					+ (extExm.getCourseID() > 10 ? extExm.getCourseID()
							: ("0" + extExm.getCourseID()))
					+ (extExm.getExamID() > 10 ? extExm.getExamID()
							: ("0" + extExm.getExamID())));
		}
		cboxExams.addActionListener(this);
	}
/**
 * 
 * @return the selected line in the combo
 */
	public int getIdFromCombo() {
		Integer temp = new Integer(examLst.get((cboxExams.getSelectedIndex()))
				.getExamID());
		return temp;

	}

	/*
	 * 
	 * @see
	 * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent e) {

		if (e.getSource() == btnGenerateExam) {
			btnGenerateExam.setEnabled(false);
			if (((examLst != null) || (!examLst.isEmpty()))
					&& (txtGenerateCode.getText().length() == 4)) {
				int comboindex = cboxExams.getSelectedIndex();
				selectedExam = examLst.get(comboindex);
				selectedExam.setExamCode(txtGenerateCode.getText());
				lecturerCtrl.GenerateExamCode(selectedExam);
				txtGenerateCode.setText("Enter exam code");
				
				

			}
	
			
		}
		if (e.getSource() ==cboxExams){
			txtExamComts.setText((examLst.get(cboxExams.getSelectedIndex())).getExamLecturerInstructions());
		}
		if (e.getSource() == btnMainMenu) {
			lecturerCtrl.handleLectutereGUI(LecturerController.MAIN);

		}
		if (e.getSource() == btnExit) {
			lecturerCtrl.handleLectutereGUI(LecturerController.EXIT);
		}

	}

	@Override
	public void focusGained(FocusEvent e) {
		if (e.getSource() == txtGenerateCode) {
			if (txtGenerateCode.getText().equals("Enter exam code"))
				txtGenerateCode.setText("");
		}

	}

	@Override
	public void focusLost(FocusEvent e) {
		if (e.getSource() == txtGenerateCode) {
			if (txtGenerateCode.getText().equals(""))
				txtGenerateCode.setText("Enter exam code");
		}

	}

	@Override
	public void keyPressed(KeyEvent e) {

	}

	@Override
	public void keyReleased(KeyEvent e) {
		if (e.isActionKey() || e.getKeyChar() == '\n')
			return;
		if (txtGenerateCode.getText().length() == 4) {
			int counter = 0;
			for (int i = 0; i < 4; i++)
				if (Character.isDigit(txtGenerateCode.getText().charAt(i)))
					counter++;
			if ((counter == 0) || (counter == 4)) {
				Perror.pError("Code must contains at least 1 digit and 1 character.");
				btnGenerateExam.setEnabled(false);
			} else
				btnGenerateExam.setEnabled(true);
		} else if (txtGenerateCode.getText().length() > 4) {
			Perror.pError("Code must be 4 characters.");
			btnGenerateExam.setEnabled(false);
		} else {
			btnGenerateExam.setEnabled(false);
		}
	}

	@Override
	public void keyTyped(KeyEvent e) {
		

	}
}
